//
//  LzwSearchViewController.m
//  CoJellyCartoon
//
//  Created by 刘志武 on 16/4/12.
//  Copyright © 2016年 CJG. All rights reserved.
//

#import "LzwSearchViewController.h"
#import <iflyMSC/iflyMSC.h>

@interface LzwSearchViewController ()<IFlyRecognizerViewDelegate>

@property (nonatomic, strong) UITextField *searchBarView; /**< 显示语音结果的lable */


@property (nonatomic, strong) IFlyRecognizerView *iFlyRecognizerView;
@property (nonatomic, strong) NSString *result;/**< 显示结果的字符串 */
@property (nonatomic, strong) UIButton *button;/**< 点击开始的录音按钮 */
@property (nonatomic, assign) BOOL isStart; /**< 是否录音的状态 */


@end

@implementation LzwSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    self.isStart = NO;
    /** 初始化语音识别空间 */
    //1.创建语音听写对象
    self.iFlyRecognizerView = [[IFlyRecognizerView alloc] initWithCenter:self.view.center];
    
    
    
    //2.设置听写参数
    [_iFlyRecognizerView setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
    
    //asr_audio_path是录音文件名,设置value为nil或者为空取消保存,默认保存目录在 Library/cache下。
    [_iFlyRecognizerView setParameter:@"asrview.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
    
    
    
    self.searchBarView = [[UITextField alloc]initWithFrame:CGRectMake(50, 5, 170, 30)];
    self.searchBarView.backgroundColor = [UIColor cyanColor];
    [self.navigationController.navigationItem setTitleView:self.searchBarView];
    
    [self.view addSubview:self.searchBarView];
    
    //创建收索框
    [self creatSearchFrame];
    
    
}

- (void)creatSearchFrame{
    
    UIImage *image = [[UIImage imageNamed:@"L17.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(0, 0, 30, 30);
    [_button setImage:image forState:UIControlStateNormal];
    
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc]initWithCustomView:_button];
    
    self.navigationItem.rightBarButtonItem = rightBar;
    
    [_button addTarget:self action:@selector(SpeakButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)SpeakButtonAction:(UIButton *)btn{
    
    _iFlyRecognizerView.delegate = self;
    [self.view addSubview:_iFlyRecognizerView];
    BOOL ret = [_iFlyRecognizerView start];
    if (ret) {
        _isStart = YES;
    }
    else {
        _isStart = NO;
    }
}

/**
 *  识别返回结果代理
 @param resultArray 识别结果
 @ param isLast 表示是否最后一次结果
 */
- (void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast {
    
    NSMutableString *resultString = [[NSMutableString alloc] init];
    NSDictionary *dic = resultArray[0];
    for (NSString *key in dic) {
        [resultString appendFormat:@"%@",key];
    }
    //(@"语音识别结果：%@", resultString);
    NSData *data = [resultString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    //(@"打印字典：%@", resultDic);
    NSArray *array_ws = [resultDic objectForKey:@"ws"];
    
    // 遍历单词
    
    NSMutableString *resultStr = [[NSMutableString alloc] init];
    for (int i = 0; i < array_ws.count; i++) {
        
        NSMutableArray *temp = [[array_ws objectAtIndex:i] objectForKey:@"cw"];
        NSDictionary * dic_cw = [temp objectAtIndex:0];
        //[resultStr  stringByAppendingString:[dic_cw objectForKey:@"w"]];
        [resultStr appendString:[dic_cw objectForKey:@"w"]];
        //(@"识别结果:%@",[dic_cw objectForKey:@"w"]);
        
    }
    //(@"最终识别结果@@@@@@@@@@@@:%@", resultStr);
    
    // 去掉识别结果的标点符号
    if ([resultStr isEqualToString:@"。"] || [resultStr isEqualToString:@"?"] || [resultStr isEqualToString:@"!"]) {
        //(@"标点符号%@", resultStr);
    }
    else {
        _searchBarView.text = resultStr;
    }
}


@end
